package com.thti.mecool.client.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.Cache;
import com.thti.mecool.client.base.HandlerConstant;
import com.thti.mecool.client.command.send.saleinto.SellIntoSend;
import com.thti.mecool.client.command.send.user.UserSend;
import com.thti.mecool.client.protocol.saleinto.SellIntoProtocol;
import com.thti.mecool.client.protocol.user.User;
import com.thti.mecool.client.service.LocationService;

public class LoginActivity extends BaseActivity {

	private ImageButton login_button;
	private String userName;
	private String passWord;
	private EditText mecool_login_username;
	private EditText mecool_login_password;
	private boolean autoLogin = false;
	private String[] names;
	SharedPreferences load_LoginInfo;
	SharedPreferences save_LoginInfo;
	ProgressDialog LoginProgress;
	CheckBox box;
	private boolean isCheck;
	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			if (!LoginProgress.isShowing()) {
				setCurrentMesg(null);
			} else {
				if (getCurrentUser() == null) {
					if (getCurrentMesg() == null) {
						handler.postDelayed(runnable, 1000);
					} else {
						Toast.makeText(LoginActivity.this,
								getCurrentMesg().getContent(), 4000).show();
						handler.post(runnable);
						LoginProgress.dismiss();
						setCurrentMesg(null);
						autoLogin = false;
						handler.removeCallbacks(runnable);
					}
				} else {
					if (isCheck) {
						save_LoginInfo = getSharedPreferences(
								"LoginPreferences", 0);
						Editor save_editor = save_LoginInfo.edit();
						save_editor.putString("userName", mecool_login_username
								.getText().toString());
						save_editor.putString("passWord", "");
						save_editor.putBoolean("autoLogin", isCheck);
						save_editor.commit();
					} else {
						save_LoginInfo = getSharedPreferences(
								"LoginPreferences", 0);
						Editor save_editor = save_LoginInfo.edit();
						save_editor.remove("userName").remove("passWord");
						save_editor.putString("userName", "");
						save_editor.putString("passWord", "");
						save_editor.putString("userId", "");
						save_editor.putBoolean("autoLogin", isCheck);
						save_editor.commit();
					}
					save_LoginInfo = getSharedPreferences("LocationService", 0);
					Editor save = save_LoginInfo.edit();
					save.putString("userName", getCurrentUser().getLoginName());
					save.commit();
					if (LoginProgress.isShowing()) {
						LoginProgress.dismiss();
					}
					handler.removeCallbacks(runnable);
					Intent i = new Intent();
					i.setClass(LoginActivity.this,
							HomeActivity.class);
					startActivity(i);
					finish();
				}
			}
		}
	};
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case HandlerConstant.GETLOGIN:
				LoginProgress = new ProgressDialog(LoginActivity.this);
				LoginProgress.setTitle("明酷督导管理系统");
				LoginProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
				LoginProgress.setMessage("正在登陆……");
				LoginProgress.show();
				UserSend.userLogin(mecool_login_username.getText().toString(),
						mecool_login_password.getText().toString());
				handler.post(runnable);
				break;

			case HandlerConstant.GETAUTOLOGIN:
				userName = load_LoginInfo.getString("userName", "null");
				passWord = load_LoginInfo.getString("passWord", "null");
				box.setChecked(true);
				if (!userName.equals("null") && !passWord.equals("null")) {
					mecool_login_username.setText(userName);
					mecool_login_password.setText(passWord);
				}
				// handler.sendEmptyMessage(HandlerConstant.GETLOGIN);
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.noTitle();
		setContentView(R.layout.mecool_login);
		Cache.add(this);
		Cache.setmContext(this);
		box = (CheckBox) findViewById(R.id.mecool_autoLogin);
		box.setOnCheckedChangeListener(listener);
		mecool_login_username = (EditText) findViewById(R.id.mecool_login_username);
		mecool_login_password = (EditText) findViewById(R.id.mecool_login_password);
		// 查询是否为自动登陆状态
		names = new String[] { "BootLoad", "AtTimeUpdate" };
		load_LoginInfo = getSharedPreferences("LoginPreferences", 0);
		autoLogin = load_LoginInfo.getBoolean("autoLogin", false);
		if (autoLogin) {
			handler.sendEmptyMessage(HandlerConstant.GETAUTOLOGIN);
		}
		login_button = (ImageButton) findViewById(R.id.mecool_login_button);
		login_button.setOnClickListener(login_button_listener);
		/**
		 * 测试用
		 */
		// User user=new User7416
	}

	@Override
	protected void onResume() {
		super.onResume();
		BaseActivity.setCurrentUser(null);
		BaseActivity.setCurrentMesg(null);
	}

	OnCheckedChangeListener listener = new OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			isCheck = isChecked;
			System.out.println("isCheck      :      " + isCheck);
		}
	};
	private OnClickListener login_button_listener = new ImageButton.OnClickListener() {
		@Override
		public void onClick(View v) {
			handler.sendEmptyMessage(HandlerConstant.GETLOGIN);
		}
	};
}
